<?php
	class GenerationController
	{
		public function getNumFiles($path)
		{
			$r = new RecursiveDirectoryIterator($path);
			foreach (new RecursiveIteratorIterator($r) as $file)
			{
				if(strpos($file->getPath(), ".svn") !== false)
					continue;
					
				$num++;
			}
			
			return $num;
		}
				
		public static function createFolder($path)
		{
			if(!is_dir($path))
				mkdir($path, 0755, true);
		}
	
		/**
		 * Delete a file, or a folder and its contents (stack algorithm)
		 *
		 * @author      Aidan Lister <aidan@php.net>
		 * @version     1.0.0
		 * @link        http://aidanlister.com/repos/v/function.rmdirr.php
		 * @param       string   $dirname    Directory to delete
		 * @return      bool     Returns TRUE on success, FALSE on failure
		 */
		public static function removeFolder($dirname)
		{
			// Sanity check
			if (!file_exists($dirname)) {
				return false;
			}
	
			// Simple delete for a file
			if (is_file($dirname) || is_link($dirname)) {
				return unlink($dirname);
			}
	
			// Create and iterate stack
			$stack = array($dirname);
			while ($entry = array_pop($stack)) {
				// Watch for symlinks
				if (is_link($entry)) {
					unlink($entry);
					continue;
				}
	
				// Attempt to remove the directory
				if (@rmdir($entry)) {
					continue;
				}
	
				// Otherwise add it to the stack
				$stack[] = $entry;
				$dh = opendir($entry);
				while (false !== $child = readdir($dh)) {
					// Ignore pointers
					if ($child === '.' || $child === '..') {
						continue;
					}
	
					// Unlink files and add directories to stack
					$child = $entry . DIRECTORY_SEPARATOR . $child;
					if (is_dir($child) && !is_link($child)) {
						$stack[] = $child;
					} else {
						unlink($child);
					}
				}
				closedir($dh);
			}
	
			return true;
		}
	
	
		public static function emptyFolder($path)
		{
			if(is_dir($path))
			{
				self::removeFolder($path);
				self::createFolder($path);
			}
			else
				self::createFolder($path);
		}
		
		
		public static function readTemplate($template)
		{
			$f = fopen(AERIAL_INTERNAL."/templates/$template.tmpl", "r");
			$contents = fread($f, filesize(AERIAL_INTERNAL."/templates/$template.tmpl"));
			return str_replace("\r\n", "\n", $contents);
		}
		
		public function writeFile($path, $contents)
		{
			$f = fopen($path, "w");
			fwrite($f, $contents);
			fclose($f);
		}
		
		public static function getTemplatePart($stub)
		{
			$parts = simplexml_load_file(AERIAL_INTERNAL."/templates/template-parts.xml");
			foreach($parts->xpath("//part") as $part)
				if($part->attributes()->name == $stub)
					return (string) $part->children()->content;
			
			return "";
		}
	}
?>